package com.java2007.zhangyu.restaurant.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.zhangyu.restaurant.dao.FoodDao;
import com.java2007.zhangyu.restaurant.dao.impl.FoodDaoImpl;
import com.java2007.zhangyu.restaurant.entity.PageBean;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.pojo.Food;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodService;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/27
 */
public class FoodServiceImpl implements FoodService {
    FoodDao foodDao = new FoodDaoImpl();

    /**
     * 保存
     * @param food 菜品类别名
     * @return
     * @throws SQLException
     */
    @Override
    public int save(Food food) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            foodDao.save(food);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    /**
     * 删除
     * @param fID 菜品类别id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByFoodId(Integer fID) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            foodDao.deleteByFoodId(fID);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    /**
     * 更新
     * @param food 菜品类别实例对象
     * @return
     * @throws SQLException
     */
    @Override
    public int update(Food food) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            foodDao.update(food);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    /**
     * 回显
     * @param fId 菜品类别id
     * @return
     * @throws SQLException
     */
    @Override
    public Food queryByFoodId(Integer fId) throws SQLException {
        return foodDao.queryByFoodId(fId);
    }

    /**
     * 根据菜品名查询菜品
     * @param fName 菜品名
     * @return
     * @throws SQLException
     */
    @Override
    public List<Food> queryByFoodName(String fName) throws SQLException {
        //判断搜索文本框中的状态
        //判断搜索文本框中是否为空
        if(StringUtils.isEmpty(fName)){
            fName = "";
        } else {
            //trim() 方法用于删除字符串的头尾空白符（空白符包括：空格、制表符 tab、换行符等其他空白符等）
            fName = fName.trim();
        }

        //此时，搜索文本框中一定有值 值为 "" 或 川菜
        return foodDao.queryByFoodName("%" + fName + "%");
    }

    @Override
    public List<Food> queryAll() throws Exception {
        return foodDao.queryAll();
    }

    @Override
    public PageBean<Food> queryPage(Integer pageNo, Integer pageSize,Food food) throws Exception {
        //判断传入的 pageNo 是否合理
        if (pageNo == 0){
            pageNo = 1;
        }

        //查询当前页面的菜品信息
        List<Food> list = foodDao.queryPage((pageNo - 1) * pageSize, pageSize,food);

        //查询菜品总数
        Long count = foodDao.getCount(food);

        PageBean<Food> pageBean = new PageBean<>();
        //当前也
        pageBean.setPageNo(pageNo);
        //页面所展示的菜品信息
        pageBean.setList(list);
        //总条数
        pageBean.setTotalCount(count);
        //总页数
        pageBean.setTotalPages();

        return pageBean;
    }

    @Override
    public int checkFoodName(String fName) {
        int result=0;
        try {
            JdbcUtils.begin();
            Food food = foodDao.selectFoodByFName(fName);
            if(food !=null){
                result=1;
            }
            JdbcUtils.commitAndClose();
        }catch (Exception e){
            JdbcUtils.rollbackAndClose();
            e.printStackTrace();
        }
        return result;
    }
}
